package com.homedo.odin.solution.service.bizservice.pc;

import com.homedo.core.common.util.BeanUtils;
import com.homedo.odin.solution.contract.mng.CommonTypeDataResp;
import com.homedo.odin.solution.contract.pc.CommonDataResponseBean;
import com.homedo.odin.solution.enumcode.HighlightEnum;
import com.homedo.odin.solution.model.dto.CommonTypeDataResponseBean;
import com.homedo.odin.solution.model.entity.CommonTypeData;
import com.homedo.odin.solution.service.CommonTypeDataServiceImpl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @author: zhanglei
 * @date: 2018/4/16
 * @Description: Pc 基础数据
 */
@Service
public class CommonDataService {

    @Autowired
    private CommonTypeDataServiceImpl commonTypeDataService;

    public List<CommonDataResponseBean> queryChildNodeById(Integer id) {
        List<CommonTypeData> commonTypeDatas = commonTypeDataService.queryChildNodeById(id);
        if (CollectionUtils.isEmpty(commonTypeDatas)) {
            return null;
        }

        List<CommonDataResponseBean> responseList = new ArrayList<>();
        for (CommonTypeData commonTypeData:commonTypeDatas) {
            CommonDataResponseBean commonDataResponseBean = new CommonDataResponseBean();
            commonDataResponseBean.setName(commonTypeData.getName());
            commonDataResponseBean.setDepth(commonTypeData.getDepth());
            commonDataResponseBean.setHighlight(commonTypeData.getHighlight());
            commonDataResponseBean.setId(commonTypeData.getId());
            commonDataResponseBean.setParentId(commonTypeData.getParentId());
            commonDataResponseBean.setPath(commonTypeData.getPath());
            responseList.add(commonDataResponseBean);
        }
        /*commonTypeData.parallelStream().map(*//*转换对象*//*data -> {
            return BeanUtils.transfrom(CommonDataResponseBean.class, data);
        }).forEach(*//*将转换后的对象添加进列表*//*responseBean -> {
            responseList.add(responseBean);
        });*/
        return responseList;
    }

    public HashMap<String, String> getDataById(int id) {
        List<CommonTypeData> childDataMapById = commonTypeDataService.getChildDataMapById(id);
        HashMap<String, String> map = new HashMap<String, String>();
        for (CommonTypeData commonTypeData : childDataMapById) {
            map.put(commonTypeData.getId() + "", commonTypeData.getName());
        }
        return map;
    }

    /**
     * 根据父节点获取可用的（高亮的）基础数据管理
     */
    public CommonTypeDataResp highlight(Integer id) {
        List<CommonTypeData> list = commonTypeDataService.queryChildNodeBy(id, HighlightEnum.YES.getDbValue());
        CommonTypeDataResp resp = new CommonTypeDataResp();
        resp.setList(BeanUtils.batchTransform(CommonTypeDataResponseBean.class, list));
        return resp;
    }
}
